/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 殇
 * Date: 2025-05-20
 * Time: 11:02
 */
//二分查找
public class code3 {
    public static void binarySearch(int[] nums,int key){
        int n=nums.length;
        int l=0,r=n-1;
        while(l<=r){
            int mid=l+(r-l)/2;
            if(nums[mid]>key)
                r=mid-1;
            else if(nums[mid]<key)
                l=mid+1;
            else{
                System.out.println("找到了，下标是:"+mid);
                return ;
            }
        }
        System.out.println("找不到");
    }
    public static void main(String[] args) {
        int[] nums=new int[]{2,4,6,8,10,14,17,21,28};
        binarySearch(nums,2);
        binarySearch(nums,15);

    }
}
